package org.qydjk.console.controller.wechatPublicNumber;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.WechatPublicExclusiveQrCode;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.RedisTemplateUtil;
import org.qydjk.common.util.wechat.WechatUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Lanan on 2017/12/28 0028.
 */
@Controller
@RequestMapping("/wechat/public/number/exclusive/qrCode")
public class WechatPublicExclusiveQrCodeController extends BaseController {

    @Autowired
    private RedisTemplateUtil redisTemplateUtil;

    @RequestMapping("/index.do")
    public String wechatPublicExclusiveQrCodeIndex() {
        return "/wechatPublicNumber/exclusiveQrCode/wechatPublicExclusiveQrCode";
    }

    @RequestMapping("/list.do")
    @ResponseBody
    public Object wechatPublicExclusiveQrCodeList(int page, int rows) {
        List<WechatPublicExclusiveQrCode> wechatPublicMessageTemplates = new ArrayList<WechatPublicExclusiveQrCode>();
        Integer total = 0;

        String sql = "SELECT * FROM qy_wechat_public_exclusive_qr_code ORDER BY update_time DESC";
        String countSql = "SELECT COUNT(id) FROM qy_wechat_public_exclusive_qr_code";
        try {
            wechatPublicMessageTemplates = baseService.findBySQL(sql, WechatPublicExclusiveQrCode.class, new HibernatePage(page, rows));
            total = baseService.getRecordCountSQL(countSql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new EasyUIResponse(total, wechatPublicMessageTemplates);
    }

    @RequestMapping("/insert.do")
    @ResponseBody
    public Object wechatPublicExclusiveQrCodeInsert(WechatPublicExclusiveQrCode wechatPublicExclusiveQrCode) {
        wechatPublicExclusiveQrCode.setCreateTime(new Date());
        return hangleInsertAndUpdate(true, wechatPublicExclusiveQrCode);
    }

    @RequestMapping("/update.do")
    @ResponseBody
    public Object wechatPublicExclusiveQrCodeUpdate(WechatPublicExclusiveQrCode wechatPublicExclusiveQrCode) {
        return hangleInsertAndUpdate(false, wechatPublicExclusiveQrCode);
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public Object wechatPublicExclusiveQrCodeDelete(WechatPublicExclusiveQrCode wechatPublicExclusiveQrCode) {
        JSONObject resp = new JSONObject();
        try {
            baseService.delete(wechatPublicExclusiveQrCode);
            resp.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            resp.put("msg", e.toString());
            resp.put("success", false);
        }
        return JSON.toJSON(resp);
    }


    private Object hangleInsertAndUpdate(boolean isInsert, WechatPublicExclusiveQrCode wechatPublicExclusiveQrCode) {
        wechatPublicExclusiveQrCode.setUpdateTime(new Date());
        JSONObject resp = new JSONObject();
        WechatPublicExclusiveQrCode oldWPEQC = new WechatPublicExclusiveQrCode();
        String sql = "SELECT COUNT(id) FROM qy_wechat_public_exclusive_qr_code WHERE k = '" + wechatPublicExclusiveQrCode.getK() + "'";
        try {
            if (!isInsert) {
                oldWPEQC = baseService.get(WechatPublicExclusiveQrCode.class, wechatPublicExclusiveQrCode.getId().intValue());
            }
            Integer count = baseService.getRecordCountSQL(sql);
            if (count > 0) {
                resp.put("success", false);
                resp.put("msg", "该专属二维码已存在,不可重复提交!");
                if (isInsert) {
                    return JSON.toJSON(resp);
                } else {
                    if (!oldWPEQC.getK().equals(wechatPublicExclusiveQrCode.getK()) ) {
                        return JSON.toJSON(resp);
                    }
                }
            }
            if (isInsert) {
                return insertNewInfo(resp, wechatPublicExclusiveQrCode);
            } else {
                return updateNewInfo(resp, wechatPublicExclusiveQrCode, oldWPEQC);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resp.put("success", false);
            resp.put("msg", e.toString());
        }
        return JSON.toJSON(resp);
    }

    /**
     * 创建微信专属二维码
     * @param id
     * @return
     */
    @RequestMapping("/create.do")
    @ResponseBody
    public Object createublicExclusiveQrCodeDelete(Integer id) {
        JSONObject resp = new JSONObject();
        try {
            WechatPublicExclusiveQrCode wechatPublicExclusiveQrCode = baseService.get(WechatPublicExclusiveQrCode.class, id.intValue());
            WechatUtil.genPerpetualQrcodeUrl(redisTemplateUtil, wechatPublicExclusiveQrCode.getK());
            resp.put("success", true);
            resp.put("msg", "生成成功");
        } catch (Exception e) {
            e.printStackTrace();
            resp.put("success", false);
            resp.put("msg", e.toString());
        }
        return JSON.toJSON(resp);
    }

    /**
     *
     * @param resp
     * @param wechatPublicExclusiveQrCode
     * @return
     */
    private Object insertNewInfo(JSONObject resp, WechatPublicExclusiveQrCode wechatPublicExclusiveQrCode) {
        try {
            resp.put("success", true);
            baseService.save(wechatPublicExclusiveQrCode);
            resp.put("msg", "添加");
        } catch (Exception e) {
            e.printStackTrace();
            resp.put("success", false);
            resp.put("msg", e.toString());
        }
        return JSON.toJSON(resp);
    }

    /**
     * 修改
     * @param resp
     * @param wechatPublicExclusiveQrCode
     * @param oldWPEQC
     * @return
     */
    private Object updateNewInfo(JSONObject resp, WechatPublicExclusiveQrCode wechatPublicExclusiveQrCode, WechatPublicExclusiveQrCode oldWPEQC) {
        try {
            BeanUtil.copyProperties(wechatPublicExclusiveQrCode, oldWPEQC);
            baseService.update(oldWPEQC);
            resp.put("msg", "修改");
            resp.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            resp.put("success", false);
            resp.put("msg", e.toString());
        }
        return JSON.toJSON(resp);
    }

}
